﻿using System.Web.UI.WebControls;
using System;
using System.IO;
//using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
//using WordApplication = Microsoft.Office.Interop.Word.Application;
using System.Reflection;
using System.Text;
using System.Collections.Generic;

public struct RTKUserInfo
{
    public string strRTKUserName;
    public double nOnLineTime;
    public RTKUserInfo(string _strName, double _nOnLineTime)
    {
        strRTKUserName = _strName;
        nOnLineTime = _nOnLineTime;
    }
}

public struct SubCORSInfoMinute
{
    public string strName;
    public List<RTKUserInfo> lsRTKUserInfo;
}

public struct SubCORSInfoMonth
{
    public string strName;
    public int nCount;
    public SubCORSInfoMonth(string _strName, int _nCount)
    {
        strName = _strName;
        nCount = _nCount;
    }
}

public struct UnitCORSInfo : IComparable
{
    public string strUnitName;
    public string strCerti;
    public int nRTKCount;
    public int nRTDCount;
    public UnitCORSInfo(string _strUnitName, string _strCerti, int _nRTKCount, int _nRTDCount)
    {
        strUnitName = _strUnitName;
        strCerti = _strCerti;
        nRTKCount = _nRTKCount;
        nRTDCount = _nRTDCount;
    }

    #region IComparable 成员

    public int CompareTo(object obj)
    {
        if (obj is UnitCORSInfo)
        {
            UnitCORSInfo ucorsi = (UnitCORSInfo)obj;
            int nthis = 0, nthat = 0;
            switch (this.strCerti.Trim())
            {
                case "甲":
                    nthis = 4;
                    break;
                case "乙":
                    nthis = 3;
                    break;
                case "丙":
                    nthis = 2;
                    break;
                case "丁":
                    nthis = 1;
                    break;
                case "无":
                    nthis = 0;
                    break;
            }
            switch (ucorsi.strCerti.Trim())
            {
                case "甲":
                    nthat = 4;
                    break;
                case "乙":
                    nthat = 3;
                    break;
                case "丙":
                    nthat = 2;
                    break;
                case "丁":
                    nthat = 1;
                    break;
                case "无":
                    nthat = 0;
                    break;
            }
            return nthat.CompareTo(nthis);
        }
        return 0;
    }

    #endregion
}

public struct CityCORSInfo
{
    public string strCityName;
    public List<UnitCORSInfo> lsUnitCORSInfo;
}

public class GlobalMethods
{
    public static void SetGridViewWidth(GridView gridview1, bool bAuto)
    {
        if (gridview1.HeaderRow == null)
        {
            //gridview1.Width = 800;
            return;
        }

        if (bAuto)
        {
            int rowcount = gridview1.Rows.Count;   //行数
            int colcount = gridview1.Columns.Count;  //列数
            int i = 0, j = 0;
            int[] cellwidth = new int[colcount];   //数组用来存储各个列的最大字符数
            int gridviewwidth = 0;  //GridView宽度
            Unit width = 0;
            string temp = null;
            int tempLength = 0;

            //for (i = 0; i < rowcount; i++)     //循环数据项，获得各个列的最大字符宽度
            //{
            //    for (j = 0; j < colcount; j++)
            //    {
            //        temp = gridview1.Rows[i].Cells[j].Text;
            //        tempLength = LengthOfLetter(temp);      //LengthOfLetter()返回含中文的字符串字符宽度，1个汉字2个字符宽
            //        if (cellwidth[j] < tempLength)
            //        {
            //            cellwidth[j] = tempLength;        //存储较大宽度值
            //        }

            //    }
            //}


            for (j = 0; j < colcount; j++)
            {
                if (gridview1.Columns[j].Visible)
                {
                    //if (gridview1.HeaderRow.Visible == true)      //如果GridView表头可见，将表头列宽参与比较                {
                    temp = gridview1.HeaderRow.Cells[j].Text;
                    if (temp == "" && gridview1.Rows.Count > 0)
                    {
                        temp = gridview1.Rows[0].Cells[j].Text;
                    }
                    if (temp.Contains("单位名") || temp.Contains("部门名"))
                    {
                        temp = "单位名称单位名称";
                    }
                    if ( temp == "权限")
                    {
                        temp = "权限权限";
                    }
                    if (temp.Contains("SIM"))
                    {
                        temp = "SIM卡号SIM卡号SIM卡号11";
                    }
                    tempLength = LengthOfLetter(temp);
                    //if (cellwidth[j] < tempLength)
                    //{
                    cellwidth[j] = tempLength;
                    //}

                }
            }

            //if (gridview1.FooterRow.Visible == true)    //如果GridView表尾可见，将表尾列宽参与比较                {
            //    temp = gridview1.FooterRow.Cells[j].Text;
            //    tempLength = LengthOfLetter(temp);
            //    if (cellwidth[j] < tempLength)
            //    {
            //        cellwidth[j] = tempLength;
            //    }
            //}

            for (j = 0; j < colcount; j++)
            {
                //if (gridview1.Columns[j].Visible == true)     //将显示的列的各列最大字符宽度相加                {
                gridview1.Columns[j].ItemStyle.Width = cellwidth[j] * 10;
                gridview1.Columns[j].ControlStyle.Width = new System.Web.UI.WebControls.Unit("90%");
                gridview1.Columns[j].ItemStyle.HorizontalAlign =System.Web.UI.WebControls.HorizontalAlign.Center;
                gridviewwidth += cellwidth[j];
            }
            gridviewwidth = gridviewwidth < 125 ? 125 : gridviewwidth;
            width = gridviewwidth * 10;  //GridView最大字符数乘于一个字符显示宽度得到GridView显示宽度
            //if (gridview1.Width.Value < width.Value)    //如果在界面上已经设置了GridView的宽度，将动态求的宽度和页面上的
            {                                                              //初始化宽度比较，如果初始化宽度较小则将宽度设置为新调整的宽度。
                gridview1.Width = width;
            }
        }
        else
        {
            double gridviewwidth = 0;
            for (int i = 0; i < gridview1.Columns.Count; i++)
            {
                gridviewwidth += gridview1.Columns[i].ItemStyle.Width.Value;
            }
            gridview1.Width = (Unit)gridviewwidth;
        }
    }

    public static void HideGridView(GridView gridview1, int[] nArr, bool bRe)
    {
        if (bRe)
        {
            for (int i = 0; i < gridview1.Columns.Count; i++)
            {
                gridview1.Columns[i].Visible = true;
            }
        }

        for (int j = 0; j < nArr.Length; j++)
        {
            gridview1.Columns[nArr[j]].Visible = false;
        }
    }

    public static void HideGridViewByName(GridView gridview1, string strHide, bool bReStart)
    {
        for (int ii = 0; ii < gridview1.Columns.Count; ii++)
        {
            gridview1.Columns[ii].Visible = true;
        }
        string[] strHeaders = strHide.Split(',');
        int j = 0;
        for (int i = 0; i < strHeaders.Length; i++)
        {
            j = bReStart ? 0 : j;
            for (; j < gridview1.Columns.Count; j++)
            {
                if (gridview1.Columns[j].HeaderText == strHeaders[i])
                {
                    gridview1.Columns[j].Visible = false;
                    j++;
                    break;
                }
            }
        }
    }

    public static string GetGridViewName(GridView gridview1)
    {
        string strTemp = string.Empty;
        for (int i = 0; i < gridview1.Columns.Count; i++)
        {
            strTemp = strTemp + "," + gridview1.Columns[i].HeaderText;
        }
        return strTemp;
    }

    //含有中文的字符串等效英文字符串显示长度
    private static int LengthOfLetter(string temp)
    {
        int length = temp.Length;
        int newlength = temp.Length;

        for (int i = 0; i < length; i++)         //遍历字符串每个字符
        {
            if (IsChineseLetter(temp, i))     //IsChineseLetter()判断是否为中文字符，是则宽度加1
            {
                newlength++;
            }
        }
        return newlength;

    }

    //判断是否为中文字符
    private static bool IsChineseLetter(string input, int index)
    {
        int code = 0;
        int chfrom = Convert.ToInt32("4e00", 16);    //范围（0x4e00～0x9fff）转换成int（chfrom～chend）
        int chend = Convert.ToInt32("9fff", 16);
        if (input != "")
        {
            code = Char.ConvertToUtf32(input, index);    //获得字符串input中指定索引index处字符unicode编码

            if (code >= chfrom && code <= chend)
            {
                return true;     //当code在中文范围内返回true

            }
            else
            {
                return false;    //当code不在中文范围内返回false
            }
        }

        return false;
    }

}




